package com.kedacom.freedomLabel.viewmodel;


import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.OnLifecycleEvent;
import android.support.annotation.Nullable;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.bean.Category;
import com.kedacom.freedomLabel.model.bean.Item;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.DataCallback;
import com.kedacom.freedomLabel.model.db.DataLoader;
import com.kedacom.freedomLabel.model.db.dao.CategoryDao;
import com.kedacom.freedomLabel.model.db.dao.ItemDao;
import com.kedacom.lego.message.LegoMessageManager;
import com.seray.scaleviewlib.utils.CacheDiskUtils;

import java.util.List;


public class ReportViewModel extends BaseViewModel {
    public MutableLiveData<String[]> mColumnNamesLiveData = new MutableLiveData<>();
    public MutableLiveData<List<Business>> mBusinessLiveData = new MutableLiveData<>();
    public MutableLiveData<Item> ID1_Item = new MutableLiveData<>();
    public MutableLiveData<Item> ID2_Item = new MutableLiveData<>();
    public MutableLiveData<Item> ID3_Item = new MutableLiveData<>();
    public MutableLiveData<Item> ID4_Item = new MutableLiveData<>();
    public MutableLiveData<Item> ID5_Item = new MutableLiveData<>();
    public MutableLiveData<Item> ID6_Item = new MutableLiveData<>();
    public MutableLiveData<List<Category>> mCategorys = new MutableLiveData<>();
    public MutableLiveData<Category> category = new MutableLiveData<>();
    public MutableLiveData<List<Item>> mItems = new MutableLiveData<>();

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void prepareItemColumnNames() {
        String usageCategory = CacheDiskUtils.getInstance(Options.LOCAL_USAGE_CATEGORY_FILE_NAME)
                .getString(Category.class.getSimpleName());
        if (usageCategory == null || usageCategory.isEmpty()) {
            LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_REPORT_NO_CATEGORY);
        } else {
            CategoryDao dao = AppDatabase.getInstance().categoryDao();
            DataLoader.get(dao, usageCategory, new DataCallback<List<Category>>() {
                @Override
                public void success(@Nullable List<Category> data) {
                    if (data != null && !data.isEmpty()) {
                         category.setValue(data.get(0));
                        String[] columnNames = category.getValue().columnNames;
                        mColumnNamesLiveData.setValue(columnNames);
                    } else {
                        LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_REPORT_NO_CATEGORY);
                    }
                }

                @Override
                public void error(@Nullable String errorMessage) {
                    LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_REPORT_NO_CATEGORY);
                }
            });
        }
    }

    public void getBusinessDataByItem(String category){

    }

    public void getAllCategory(){
        CategoryDao categoryDao = AppDatabase.getInstance().categoryDao();
        DataLoader.get(categoryDao, new DataCallback<List<Category>>() {
            @Override
            public void success(@Nullable List<Category> data) {
                mCategorys.setValue(data);
            }

            @Override
            public void error(@Nullable String errorMessage) {

            }
        });
    }


    public void getCurCategoryItems(String  parentid,String category){
        ItemDao itemDao = AppDatabase.getInstance().itemDao();
        DataLoader.get(itemDao, parentid, category, new DataCallback<List<Item>>() {
            @Override
            public void success(@Nullable List<Item> data) {
                mItems.setValue(data);
            }

            @Override
            public void error(@Nullable String errorMessage) {

            }
        });

    }

}
